<?php

namespace App\Http\Controllers;

use App\Commodity;
use App\Datatables;
use App\Promotion;
use Illuminate\Http\Request;

class PromotionController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
    }

    /**
     * 团购成功列表
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\View\View
     */
    public function promotionList(Request $request)
    {
        if ($request->isMethod('post')) {
            return response()->json(Datatables::PromotionList());
        }

        return view('promotion.promotionList');
    }


    /**
     * 某商品的团购人员列表
     * @param Request $request
     * @param $commodity_id
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|\Illuminate\View\View
     */
    public function promotionSendList(Request $request, $commodity_id)
    {
        //商品是否存在
        if (!Commodity::where('id', $commodity_id)->exists()) {
            return redirect('/promotion/promotionList');
        }

        if ($request->isMethod('post')) {
            return response()->json(Datatables::promotionSendList($commodity_id));
        }

        return view('promotion.promotionSendList');
    }

    /**
     * 发货
     * @param Request $request
     * @return array
     */
    public function promotionSend(Request $request)
    {
        if (!$request->has('id') ||!$request->has('delivery')) {
            return ['status' => 2, 'message' => '缺少参数'];
        }
        return Promotion::PromotionSend($request);
    }
  
  
  /**
     * 订单管理
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\View\View
     */
    public function orderList(Request $request)
    {
        if ($request->isMethod('post')) {
            return response()->json(Datatables::OrderList());
        }

        return view('promotion.orderList');
    }
}
